//
//  SMHMineProductCell.swift
//  SMHCommerce
//
//  Created by lvzhao on 2020/6/3.
//  Copyright © 2020 lvzhao. All rights reserved.
//

import UIKit
import SwiftyFitsize

class SMHMineProductCell: UICollectionViewCell {
    
    var vipView = UIView()
    var vipNumLab = UILabel()
    
    override init(frame: CGRect) {
         super.init(frame: frame)
         
         setupUI()
         
     }
     required init?(coder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
     
    
    func setupUI() {
           
           let invite = 40~
           let itemW = (kScreenWidth - invite)/2.0
           self.contentView .addSubview(self.imageView)
           self.imageView.snp.makeConstraints { (make) in
               make.left.equalTo(15~)
               make.width.equalTo(itemW - 30~)
               make.height.equalTo(self.imageView.snp.width)
               make.top.equalTo(15~)
           }
           
           
           //商品名称
           self.contentView .addSubview(self.titleLab)
           self.titleLab.snp.makeConstraints { (make) in
               make.left.equalTo(self.imageView)
               make.width.equalTo(self.imageView)
               make.top.equalTo(self.imageView.snp.bottom).offset(10~)
               make.height.equalTo(32~)
           }
           
           
           //商品价格
           self.contentView .addSubview(self.priceLab)
           self.priceLab.snp.makeConstraints { (make) in
               make.left.equalTo(self.imageView)
               make.width.equalTo(self.imageView)
               make.top.equalTo(self.titleLab.snp.bottom).offset(8~)
               make.height.equalTo(15~)
           }
           
           //会员价View
           self.vipView = UIView()
           self.contentView .addSubview(self.vipView)
           self.vipView.snp.makeConstraints { (make) in
               make.top.equalTo(self.priceLab.snp.bottom).offset(8~)
               make.left.equalTo(self.imageView)
               make.width.equalTo(85)
               make.height.equalTo(13~)
           }
           
           self.vipView.layer.cornerRadius = (13/2)
           self.vipView.clipsToBounds = true
           self.vipView.layer.shadowRadius = (13/2)
           self.vipView.layer.borderWidth = 0.5
           self.vipView.layer.borderColor = UIColorFromHex(rgbValue: 0x0D87F4).cgColor
           
           let vipLab = UILabel()
           vipLab.textColor = UIColor.white
           vipLab.text = "会员价"
           vipLab.textAlignment = .center
           vipLab.font = ktextFont(size: 9)
           vipLab.backgroundColor = UIColorFromHex(rgbValue: 0x0D87F4)
           self.vipView.addSubview(vipLab)
           vipLab.snp.makeConstraints { (make) in
               make.left.equalTo(0)
               make.centerY.equalTo(self.vipView.snp.centerY)
               make.width.equalTo(35)
               make.height.equalTo(13)
           }
           
           self.vipView.addSubview(self.vipPriceLab)
           self.vipPriceLab.snp.makeConstraints { (make) in
               make.left.equalTo(vipLab.snp.right).offset(3)
               make.top.equalTo(0)
               make.height.equalTo(13)
               make.right.equalTo(self.vipView.snp.right)
           }
           
       }
       
       var modelObject : AnyObject?{
           didSet{

               print("didSet")
               print(modelObject as Any)
               self.titleLab.text = "Apple iPhone 11 苹果手机白色全网通128GB";
               let url = URL(string: "http://gss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/267f9e2f07082838685c484ab999a9014c08f11f.jpg")

               let image = UIImage(named: "default_profile_icon")
               self.imageView.image = image
               self.imageView.kf.setImage(with: url)
               self.imageView.kf.indicatorType = .activity
               //  Converted to Swift 5.2 by Swiftify v5.2.19603 - https://swiftify.com/
               //单价
               var price = "89.00"
               price = "¥ \(price)"
               let priceAttriString = NSMutableAttributedString(string: price)
               let surplusDic = [NSAttributedString.Key.font: ktextFont(size: 10)]
               let range = NSRange(location: price.count - 2, length: 2)
               priceAttriString.addAttributes(surplusDic, range: range)
               self.priceLab.attributedText = priceAttriString
               
               
               var vipPrice = "2245.00"
               vipPrice = "¥ \(vipPrice)"
               let vipPriceAttriString = NSMutableAttributedString(string: vipPrice)
               let surplusVipDic = [NSAttributedString.Key.font: ktextFont(size :10)]
               let vipRange = NSRange(location: vipPrice.count - 2, length: 2)
               vipPriceAttriString.addAttributes(surplusVipDic, range: vipRange)
               self.vipPriceLab.attributedText = vipPriceAttriString
               
               let vipPriceSize : CGSize = self.vipPriceLab.sizeThatFits(CGSize(width: 200, height: 13))
               var viptTotalWidth = vipPriceSize.width + 45.0
               if viptTotalWidth < 85 {
                   viptTotalWidth = 85
               }
           
               self.vipView.snp.updateConstraints { (make) in
                   make.width.equalTo(viptTotalWidth)
               }
           }
       }


       
       //MARK: 懒加载
       lazy var imageView : UIImageView  = {
           let imageView = UIImageView(image: UIImage(named: "icon_product_defulat"));
           return imageView
       }()
       
       lazy var titleLab : UILabel = {
           let titleLab = UILabel ()
           titleLab.textColor = UIColorFromHex(rgbValue: 0x363636)
           titleLab.font = ktextFont(size: 13)~
           titleLab.numberOfLines = 0
           return titleLab
       }()
       
       
       lazy var priceLab : UILabel = {
           let priceLab = UILabel ()
           priceLab.textColor = ColorTint
           priceLab.font = ktextBoldFont(size: 16~)
           return priceLab
       }()
       
       
       lazy var vipPriceLab : UILabel = {
           let vipPriceLab = UILabel ()
           vipPriceLab.textColor = UIColorFromHex(rgbValue: 0x0D87F4)
           vipPriceLab.font = ktextFont(size: 10~)
           return vipPriceLab
       }()
           
}
